package com.yonyou.pmclouds.workstopreport.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonProgramConsts;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.copyperson.rmiitf.ICopyPersonQuery;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.pmpub.entity.QueryListParam;
import com.yonyou.pmclouds.pmpub.rmiitf.IPageQuerySerivce;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.mapper.WorkStopMapper;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopQuery;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopQuery.class)
public class WorkStopQueryImpl implements WorkStopQuery, IPageQuerySerivce<WorkStopVO> {

    @Autowired
    private WorkStopMapper workStopMapper;

    @Autowired
    private PictureRemoteService picService;

    @Autowired
    private ICopyPersonQuery copyPersonQuery;


    @Override
    public WorkStopVO[] queryByProject(QueryListParam param) throws BusinessException {
        WorkStopVO[] vos = workStopMapper.queryByCondition(new String[]{param.getPkProject()}, null,  param.getStartDate(), param.getEndDate(), null,param.getPageStart(),param.getPageSize());

        if(null == vos || vos.length == 0){
            return new WorkStopVO[0];
        }
        getPics(vos);

        return vos;
    }

    private void getPics(WorkStopVO[] vos) throws BusinessException {
        List<String> entityies = new ArrayList<>();
        for(WorkStopVO workStopVO : vos){
            entityies.add(workStopVO.getPkWorkstopreport());
        }
        Map<String, List<String>> entitiesForPic = picService.queryPicsByPkEntitys(entityies.toArray(new String[0]));
        for(WorkStopVO workStopVO : vos){
            List<String> pics = entitiesForPic.get(workStopVO.getPkWorkstopreport());
            if(null == pics || pics.size() == 0){
                continue;
            }
            workStopVO.setPictures(pics.toArray(new String[0]));
            workStopVO.setPicNum(pics.size());
        }
    }

    @Override
    public int countByProject(QueryListParam param) throws BusinessException {
        int count = workStopMapper.countByCondition( new String[]{param.getPkProject()}, null,  param.getStartDate(), param.getEndDate(), null);

        return count;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.WORKSTOP_BILLTYPE;
    }

    @Override
    public Integer counts() {
        return workStopMapper.counts();
    }

    @Override
    public ProjectVO getProjectById(String projectId) {
        ProjectVO projectVO = workStopMapper.selectProject(projectId);
        return projectVO;
    }

    @Override
    public PageObject<Object> queryByCondition(String[] pkProject, String[] creators, String startDate, String endDate, int[] billStatus, int start, int size) throws BusinessException {
        int count = workStopMapper.countByCondition(pkProject, creators, startDate, endDate, billStatus);
        WorkStopVO[] workStopVOS = workStopMapper.queryByCondition(pkProject, creators, startDate, endDate, billStatus, start, size);
        for (WorkStopVO w : workStopVOS) {
            //获取项目主键
            String pk = w.getPkProject();
            ProjectVO projectVO = workStopMapper.selectProject(pk);
            //赋值项目名
            w.setProjectName(projectVO.getSname());
        }
        //获取图片
        getPics(workStopVOS);
        //获取制单人


        return new PageObject<Object>(workStopVOS, count, start);
    }

    @Override
    public Map<String,List<WorkStopVO>> queryByProjects(String[] projectIds) {
        WorkStopVO[] workStopVOS = workStopMapper.queryByProjects(projectIds);
        if(null == workStopVOS || workStopVOS.length == 0){
            return new HashMap<>();
        }
        Map<String,List<WorkStopVO>> map = new HashMap<>();
        for(WorkStopVO workStopVO : workStopVOS){
            List<WorkStopVO> list = new ArrayList();
            if(null == map.get(workStopVO.getPkProject())){
                map.put(workStopVO.getPkProject(),list);
            }else{
                List<WorkStopVO> temp = map.get(workStopVO.getPkProject());
                temp.add(workStopVO);
                map.put(workStopVO.getPkProject(),temp);
            }
        }
        return map;
    }

    @Override
    public Map<String, WorkStopVO> queryByIds(String[] ids) {
        //强制截取长度，避免insql可能的问题
        if (ids.length > CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE) {
            ids = Arrays.copyOf(ids, CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE);
        }

        WorkStopVO[] workStopVOS = workStopMapper.queryByIds(ids);

        if(null == workStopVOS || workStopVOS.length == 0){
            return new HashMap();
        }

        Map<String,WorkStopVO> map = new HashMap<>();

        for(WorkStopVO workStopVO : workStopVOS){
            map.put(workStopVO.getPkWorkstopreport(),workStopVO);
        }
        return map;
    }

    @Override
    public ProjectVO[] getProject(String status) {
        return new ProjectVO[0];
    }

    @Override
    public WorkStopVO getReport(String reportId) throws BusinessException {
        String[] pics = picService.queryPicNamesByPkEntity(reportId);
        WorkStopVO workStopVO = workStopMapper.selectReport(reportId);
        workStopVO.setPictures(pics);
        workStopVO.setPicNum(pics.length);
        //判断单据状态
        if (workStopVO.getBillstatus() != null) {
            switch (workStopVO.getBillstatus()) {
                case BillStatusConst.COMMON_APPROVING_STATUS:
                    workStopVO.setStatus("审批中");
                    break;
                case BillStatusConst.COMMON_REJECT_STATUS:
                    workStopVO.setStatus("驳回");
                    break;
                case BillStatusConst.COMMON_APPROVED_STATUS:
                    workStopVO.setStatus("审批通过");
                    break;
                case BillStatusConst.COMMON_COMMIT_STATUS:
                    workStopVO.setStatus("驳回后再提交");
                    break;
                case BillStatusConst.COMMON_FREE_STATUS:
                    workStopVO.setStatus("自由态");
            }
        }

        List<String> list = new ArrayList<>();
        list.add(reportId);
        List<CopyPersonVO> copyPersonVOS = copyPersonQuery.queryByPkBills(list);
        if(null != copyPersonVOS && copyPersonVOS.size() > 0){
            workStopVO.setCopyPersonVOList(copyPersonVOS);
        }

        //根据项目Id获取项目名以及项目编号
        String pkProject = workStopVO.getPkProject();
        ProjectVO projectVO = workStopMapper.selectProject(pkProject);
        workStopVO.setProjectName(projectVO.getSname());
        workStopVO.setProjectCode(projectVO.getScode());

        return workStopVO;
    }

    @Override
    public WorkStopVO[] getAllCondition(String projectId) {
        return new WorkStopVO[0];
    }

    @Override
    public WorkStopVO[] getAllBill(String projectId) {
        return new WorkStopVO[0];
    }
}
